/*     */ package com.zimbra.qa.unittest;
/*     */ 
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import java.io.BufferedReader;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.io.InputStreamReader;
/*     */ import java.io.PrintWriter;
/*     */ import java.net.ServerSocket;
/*     */ import java.net.Socket;
/*     */ import java.util.ArrayList;
/*     */ import java.util.List;
/*     */ import java.util.regex.Matcher;
/*     */ import java.util.regex.Pattern;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class DummySmtpServer
/*     */   implements Runnable
/*     */ {
/*     */   private int mPort;
/*     */   private String mRejectRcpt;
/*     */   private String mErrorMsg;
/*     */   private PrintWriter mOut;
/*  41 */   private List<String> mDataLines = new ArrayList();
/*     */   private String mMailFrom;
/*  43 */   private static final Pattern PAT_RCPT = Pattern.compile("RCPT TO:<(.*)>", 2);
/*  44 */   private static final Pattern PAT_MAIL_FROM = Pattern.compile("MAIL FROM:<(.*)>", 2);
/*     */   
/*     */   public DummySmtpServer(int port) {
/*  47 */     this.mPort = port;
/*     */   }
/*     */   
/*     */   void setRejectedRecipient(String rcpt, String error) {
/*  51 */     this.mRejectRcpt = rcpt;
/*  52 */     this.mErrorMsg = error;
/*     */   }
/*     */   
/*     */   public String getMailFrom() {
/*  56 */     return this.mMailFrom;
/*     */   }
/*     */   
/*     */   public List<String> getDataLines() {
/*  60 */     return this.mDataLines;
/*     */   }
/*     */   
/*     */   public void run() {
/*  64 */     ServerSocket server = null;
/*  65 */     Socket socket = null;
/*  66 */     InputStream in = null;
/*     */     try {
/*  68 */       server = new ServerSocket(this.mPort);
/*  69 */       socket = server.accept();
/*  70 */       in = socket.getInputStream();
/*  71 */       this.mOut = new PrintWriter(socket.getOutputStream());
/*  72 */       BufferedReader reader = new BufferedReader(new InputStreamReader(in));
/*  73 */       String line = null;
/*     */       
/*  75 */       send("220 " + DummySmtpServer.class.getSimpleName());
/*  76 */       while ((line = reader.readLine()) != null) {
/*  77 */         String uc = line.toUpperCase();
/*  78 */         if (uc.startsWith("MAIL FROM")) {
/*  79 */           Matcher m = PAT_MAIL_FROM.matcher(line);
/*  80 */           if (m.matches()) {
/*  81 */             this.mMailFrom = m.group(1);
/*     */           }
/*  83 */           send("250 OK");
/*  84 */         } else if (uc.startsWith("DATA")) {
/*  85 */           send("354 OK");
/*  86 */           line = reader.readLine();
/*  87 */           while (!line.equals(".")) {
/*  88 */             this.mDataLines.add(line);
/*  89 */             line = reader.readLine();
/*     */           }
/*  91 */           send("250 OK");
/*  92 */         } else { if (uc.startsWith("QUIT")) {
/*  93 */             send("221 Buh-bye.");
/*  94 */             break; }
/*  95 */           if (uc.startsWith("RCPT")) {
/*  96 */             Matcher m = PAT_RCPT.matcher(line);
/*  97 */             if ((m.matches()) && (m.group(1).equals(this.mRejectRcpt))) {
/*  98 */               send("550 " + this.mErrorMsg);
/*     */             } else {
/* 100 */               send("250 OK");
/*     */             }
/*     */           } else {
/* 103 */             send("250 OK");
/*     */           }
/*     */         }
/*     */       }
/*     */       return; } catch (Exception e) { ZimbraLog.test.error("Error in %s.", DummySmtpServer.class.getSimpleName(), e);
/*     */     } finally {
/*     */       try {
/* 110 */         if (this.mOut != null) {
/* 111 */           this.mOut.close();
/*     */         }
/* 113 */         if (in != null) {
/* 114 */           in.close();
/*     */         }
/* 116 */         if (socket != null) {
/* 117 */           socket.close();
/*     */         }
/* 119 */         if (server != null) {
/* 120 */           server.close();
/*     */         }
/*     */       } catch (IOException e) {
/* 123 */         e.printStackTrace();
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   private void send(String response) {
/* 129 */     this.mOut.print(response + "\r\n");
/* 130 */     this.mOut.flush();
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/DummySmtpServer.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */